import api from '../../../api/index';
Page({
	/**
   * 页面的初始数据
   */
	data: {
    getvideolist : null,
    defaultImageUrl: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/pages/error2.png',
    type: false,
    showVideolist: []
	},

	/**
   * 生命周期函数--监听页面加载
   */
	onLoad(options) {
    const {type} = options 
    if (type) {
      this.setData({
        type: true
      });
    }
  },
  handleImageError: function(e) {
    console.log(e);
    this.setData({
      imageUrl: this.data.defaultImageUrl // 将图片路径设置为默认图片路径
    });
  },
  errorFunction: function (event) {
    console.log(event);
    var index = event.currentTarget.dataset.index
    var images = 'getvideolist['+ index +'].images'
    this.setData({
      [images]: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/pages/error2.png'
    })
  },
	/**
     * 获取视频列表
     */
	async getvideolist() {
		// console.log('getBanner')
		try {
			const getvideolist = await this.getvideoload();
      if(getvideolist) {
        for (let i = 0; i < getvideolist.length; i++) {
          if(!getvideolist[i].images) {
            getvideolist[i].images = 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/pages/error2.png'
          }
        }
        let showList = []
        if(this.data.type == true) {
          showList = getvideolist.filter(item => {
            return item.collect == '1'
          })
        } else {
          showList = getvideolist
        }
        this.setData({
          showVideolist: showList
        });
      }
		
		} catch (e) {
			console.error(e);
		}
  },
  async getvideoload() {
    try {
      const res = await api.post('/api/hxhealth/video/list', {
          data: {
              'name': ''
          }
      })
      return res
  } catch (e) {
      console.log('getvideoload->', e)
      return null
  }
  },
  go(e) {
    console.log(e.currentTarget.dataset);
    const {titlename,video,content,id} = e.currentTarget.dataset
    console.log(titlename);
    wx.navigateTo({
      url: `/videoplayer/pages/video/index?titlename=${titlename}&articleId=${id}&video=${video}&content=${content}`
  })
  },
	/**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady() {},

	/**
   * 生命周期函数--监听页面显示
   */
	onShow() {
    this.getvideolist();
  },

	/**
   * 生命周期函数--监听页面隐藏
   */
	onHide() {},

	/**
   * 生命周期函数--监听页面卸载
   */
	onUnload() {},

	/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh() {},

	/**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom() {},

	/**
   * 用户点击右上角分享
   */
	onShareAppMessage() {}
});
